﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Kaidou.Persister;
using Kaidou.Model;
using System.Collections.Generic;
using Kaidou.Util;
using Kaidou.Bll;

public partial class master_master_union_detail : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        LoginModel loginModel = (LoginModel)Session["loginModel"];

        if (loginModel == null)
        {
            Response.Redirect("../login.aspx");
        }
        else if (!loginModel.isOperator() && !loginModel.isJimukyoku())
        {
            Response.Redirect("../menu.aspx");
        }

        if ("on".Equals(Resources.Resource.kaidou_view_user_clss))
        {
            user_clss_panel.Visible = true;
        }
        else
        {
            user_clss_panel.Visible = false;
        }

        if (loginModel != null && loginModel.isOperator())
        {
            updateButton.Visible = true;
            deleteButton.Visible = true;
        }
        

        

        if (!IsPostBack)
        {
            string area = Resources.Resource.area;
            string[] areaArray = area.Split(',');
            for (int i = 0; i < areaArray.Length; i++)
            {
                ListItem item = new ListItem();
                item.Value = (i + 1) + "";
                item.Text = areaArray[i];
                office_pref_code.Items.Insert(i + 1, item);
            }

            MST_UnionPersister dao = new MST_UnionPersister();
            MstUnion M = dao.getDetail(Request.QueryString["unioncode"].ToString());
            Session["mstunion"] = M;

            union_code.Text = M.Id.UnionCode;
            office_address.Text = M.OfficeAddress;
            office_city.Text = M.OfficeCity;
            office_email.Text = M.OfficeEmail;
            office_fax.Text = M.OfficeFax;
            office_kana.Text = M.OfficeKana;
            office_name.Text = M.OfficeName;
            office_pref_code.SelectedIndex = M.OfficePrefCode;
            office_tel.Text = M.OfficeTel;
            office_zip_no1.Text = M.OfficeZipNo.Substring(0, 3);
            office_zip_no2.Text = M.OfficeZipNo.Substring(3, 4);
            tanto_kana.Text = M.TantoKana;
            tanto_name.Text = M.TantoName;
            if (M.TraderRegNo != null && M.TraderRegNo.Trim().Length > 0)
            {
                trader_reg_no1.Text = M.TraderRegNo.Substring(0, 2);
                trader_reg_no2.Text = M.TraderRegNo.Substring(2, 5);
            }
           
            daihyo_name.Text = M.DaihyoName;
            daihyo_kana.Text = M.DaihyoKana;

            if (int.Parse(M.UserClss) == 10)
            {
                user_clss1.Checked = true;
            }
            if (int.Parse(M.UserClss) == 20)
            {
                user_clss2.Checked = true;
            }
            if (int.Parse(M.UserClss) == 30)
            {
                user_clss3.Checked = true;
            }
            password.Text = M.Password;
        }

    }

    protected void delete(object sender, EventArgs e)
    {
        MstUnion M = (MstUnion)Session["mstunion"];

        M.UpdDate = System.DateTime.Now;
        M.UpdUnionCode = (string)Session["Login_Union_code"];
        M.UpdBranchCode = (short)Session["Login_Branch_code"];
        M.UpdFuncId = "deleteMasterUnion()";
        M.UnionSts = StringUtil.STATUS_DELETE;
        
        M.UserClss = (user_clss1.Checked ? 10 : user_clss2.Checked ? 20 : 30)+"";
        
        
        Session["mstunion"] = M;
        Response.Redirect("master_union_delete_conf.aspx");
       
        
    }

    protected void update(object sender, EventArgs e)
    {

        //カプセル化オブジェクト
        MstUnion M = (MstUnion)Session["mstunion"];
        MstUnionId MID = new MstUnionId();
        MID.UnionCode = union_code.Text;
        MID.BranchCode = 0;
        M.Id = MID;
        M.OfficeAddress = office_address.Text;
        M.OfficeCity = office_city.Text;
        M.OfficeEmail = office_email.Text;
        M.OfficeFax = office_fax.Text;
        M.OfficeKana = office_kana.Text;
        M.OfficeName = office_name.Text;
        M.OfficePrefCode = short.Parse(office_pref_code.Text);
        M.OfficeTel = office_tel.Text;
        M.OfficeZipNo = office_zip_no1.Text + office_zip_no2.Text;
        M.OfficeZipNo1 = office_zip_no1.Text;
        M.OfficeZipNo2 = office_zip_no2.Text;
        M.Password = password.Text;
        M.TantoKana = tanto_kana.Text;
        M.TantoName = tanto_name.Text;
        M.DaihyoName = daihyo_name.Text;
        M.DaihyoKana = daihyo_kana.Text;
        M.TraderRegNo = trader_reg_no1.Text + trader_reg_no2.Text;
        M.TraderRegNo1 = trader_reg_no1.Text;
        M.TraderRegNo2 = trader_reg_no2.Text;

        if (!StringUtil.CANCEL_REST_FLG_FORCE.Equals(M.CancelRestFlg))
        {
            M.CancelRestFlg = StringUtil.CANCEL_REST_FLG_NORMAL;
        }

        M.UpdDate = System.DateTime.Now;
        M.UpdUnionCode = (string)Session["Login_Union_code"];
        M.UpdBranchCode = (short)Session["Login_Branch_code"];
        M.UpdFuncId = "updateMasterUnion()";

        M.UserClss = (user_clss1.Checked ? 10 : user_clss2.Checked ? 20 : 30) + "";


        //検証
        MST_UnionManager validation = new MST_UnionManager();
        List<string> errorCodeList = validation.checkInsertOrUpdateUnion(M, Resources.Resource.ResourceManager,false);
        msg.Text = ErrorCodeUtil.errorCodeListToHtml(errorCodeList);

        if (errorCodeList.Count == 0)
        {
            Session["mstunion"] = M;
            Response.Redirect("master_union_update_conf.aspx");
        }



    }

    protected void goback(object sender, EventArgs e)
    {
        Session["back"] = 1;
        Response.Redirect("master_union_search.aspx");

    }
}
